package com.zjh.admin.sys.service.impl;

import com.zjh.admin.sys.mapper.RoleMapper;
import com.zjh.admin.sys.model.RoleEntity;
import com.zjh.admin.sys.service.RoleService;
import com.zjh.core.base.baseservice.impl.AbstractService;
import com.zjh.core.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ZWX
 * @Description: 角色service实现
 * @date 2018/11/12 17:44
 */
@Service("roleService")
public class RoleImplService extends AbstractService<RoleEntity , Long> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    public void setBaseMapper() {
        super.setBaseMapper(roleMapper);
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteRoleById(Long id) {
        try {
            // 1、删除该角色的权限信息
            roleMapper.deleteRoleResource(id.intValue());
            // 2、删除该角色
            if (roleMapper.deleteById(id) > 0) {
                return Boolean.TRUE;
            } else {
                return Boolean.FALSE;
            }
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 查询该角色下是否有用户
     *
     * @param roleId
     * @return
     */
    @Override
    public int findRoleUserById(int roleId) {
        return roleMapper.findRoleUserById(roleId);
    }

    /**
     * 添加角色和权限映射信息
     *
     * @param roleId
     * @param resourceId
     * @return
     */
    @Override
    public boolean addRolePerm(Long roleId, Long resourceId) {
        try {
            Map<String, Object> parameter = new HashMap<>(16);
            parameter.put("roleId", roleId);
            parameter.put("resourceId", resourceId);
            return roleMapper.addRoleResource(parameter) > 0;
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 批量添加角色和权限映射信息
     * @param id
     * @param ids
     * @return
     */
    @Override
    public boolean addRolePermBatch(int id, List<Integer> ids) {
        boolean flag = false;
        try {
            int permCount = roleMapper.findRoleResourceById(id);
            boolean delFlag = true;
            if (permCount > 0) {
                int delResult = roleMapper.deleteRoleResource(id);
                if (permCount != delResult) {
                    delFlag = false;
                }
            }

            if (delFlag) {
                if (ids.size() > 0) {
                    Map<String, Object> parameter = new HashMap<>(16);
                    parameter.put("roleId", id);
                    parameter.put("resourceIds", ids);
                    int addResult = roleMapper.addRoleResourceBatch(parameter);
                    if (addResult == ids.size()) {
                        flag = true;
                    }
                } else {
                    flag = true;
                }
            }

            List<Long> userIds = roleMapper.findUserIdByRoleId(id);

            return flag;
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }
}
